package test.h0820;

import java.util.*;

public class Permutations {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] nums = Arrays.stream(sc.nextLine().split(",")).mapToInt(Integer::parseInt).toArray();
        List<List<Integer>> permutations = permuteUnique(nums);
        for (List<Integer> permutation : permutations) {
            System.out.println(permutation);
        }
    }

    public static List<List<Integer>> permuteUnique(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        Arrays.sort(nums); // 排序是为了剪枝
        boolean[] used = new boolean[nums.length];
        backtrack(nums, result, new ArrayList<>(), used);
        return result;
    }

    private static void backtrack(int[] nums, List<List<Integer>> result, List<Integer> tempList, boolean[] used) {
        if (tempList.size() == nums.length) {
            result.add(new ArrayList<>(tempList));
            return;
        }
        
        for (int i = 0; i < nums.length; i++) {
            // 如果当前元素已经使用过，跳过
            if (used[i]) continue;
            // 如果当前元素与前一个元素相同，且前一个元素没有使用过，跳过
            // 这样可以避免产生重复的排列
            if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1]) continue;
            
            tempList.add(nums[i]);
            used[i] = true;
            backtrack(nums, result, tempList, used);
            used[i] = false;
            tempList.remove(tempList.size() - 1);
        }
    }
}
